<template>
  <TabHead/>
  <div class="box">
    <h1 style="text-align: center">注册</h1>
    <el-form size="large" :rules="rules" :model="user" ref="ruleFormRef">
      <el-form-item prop="username" label="昵称">
        <el-input v-model="user.username" style="margin-left: 14px" placeholder="填写你中意的网名吧"/>
      </el-form-item>
      <el-form-item prop="user_id" label="用户名">
        <el-input v-model="user.user_id" placeholder="最好使用数字和英文组合"/>
      </el-form-item>
      <el-form-item prop="pwd" label="密码" style="margin-left: 14px">
        <el-input v-model="user.pwd" show-password placeholder="越复杂越好哦"/>
      </el-form-item>
      <el-button type="primary" style="width: 87%;margin-left: 65px" @click="regist()">
        注册
      </el-button>
    </el-form>
  </div>
</template>

<script setup>
import {getCurrentInstance, reactive} from 'vue'
import {ElMessage} from "element-plus";
import request from "../request";
import {useRouter} from "vue-router";
import TabHead from "../components/TabHead.vue";

//创建对象user
const user = reactive({
  perms: 'normal'
})

//定义表单校验规则
const rules = reactive({
  user_id: [
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  pwd: [
    {required: true, message: '请输入密码', trigger: 'blur'}
  ],
  username: [
    {required: true, message: '请输入昵称', trigger: 'blur'}
  ]
})


// 往后台发请求 http://localhost:9090
const {proxy} = getCurrentInstance()
const router = useRouter();

const regist = () => {
  proxy.$refs.ruleFormRef.validate((valid) => {
    if (valid) {
      request.post('/user/add', user).then((res) => {
        if (res === true) {
          ElMessage({
            message: '注册成功',
            type: 'success',
          })

          router.push("/Login")
        } else if (res.success === false) {
          ElMessage.error('注册失败')
        }
      })

    }
  })
}
</script>

<style>
.box {
  width: 500px;
  margin: 150px auto;
}
</style>

